function rslice_vslice_redraw( hObject, options )
% RSLICE_VSLICE_REDRAW:  redraws the vertical slice, if it exists.
%
% PARAMETERS
% Input:
%     hObject:  this should be the hslice figure
%     options:
%         Optional.  If given, this structure will have flags that
%         override the default flags.
%
%
% The line
%
%     gdata = guidata ( hObject );
%
% retrieves a global data structure used to store information.
% This routine sets a field of gdata called "vslice_data".  This 
% field is a structure itself and contains the following fields.
%
%     vslice_line_handles:
%         Four lines that depict the vslice velocity line on the 
%         hslice figure.  The first is a thin high resolution line.
%         The second is a thick high resolution line that fits 
%         underneath the first.  The third is a line of asterisks
%         at the control points defining the line.  These asterisks
%         fit into the fourth line, which is just a set of 'o'
%         markers.
%     spline_x, spline_y:
%         Structure that define the splined lines whose handles 
%         are stored in "vslice_line_handles".
%     vx:
%         2D array.  x array along the velocity slice
%     vz:
%         2D array.  z (or depth) array along the velocity slice
%     vdata:
%         2D array. This is the velocity slice itself, vdata = u + i*v
%     transect_dist:
%         1D array, cumulative distance along the slice at the surface.
%     control_pt_x, control_pt_y:
%         points that the user specified to determine the vslice line
%     hires_x, hires_y:
%         splined high resolution points along the vslice line
% 
% Calls:
%     rslice_get_grid
%


%
% Set defaults.
% These flags control whether or not we need to get new information.
retrieve_new_data = 0;
get_new_line = 0;
use_vslice_vel_line = 0;


%
% Override the defaults here.
if nargin > 1

	if isfield ( options, 'get_new_line' )
		get_new_line = options.get_new_line;
	end

	if isfield ( options, 'use_vslice_vel_line' )
		use_vslice_vel_line = options.use_vslice_vel_line;
	end

	if isfield ( options, 'retrieve_new_data' )
		retrieve_new_data = options.retrieve_new_data;
	end

end

gdata = guidata ( hObject );
handles = guihandles ( hObject );

%
% Does the figure even exist?  If not, then create it.
try
	get ( gdata.vslice_figure, 'visible' );
catch
	gdata.vslice_figure = vslice;
	set ( gdata.vslice_figure, 'visible', 'on' );
end

vslice_handles = guidata(gdata.vslice_figure);

%
% If the vslice velocity figure is visible, delete it and the vslice line
h = findobj ( gdata.horizontal_slice_axis, 'tag', 'VSlice Line' );
if ~isempty(h)
	delete(h);
end









%
% It's possible we arrive here with data not appropriate for vertical
% slicing.  If that's the case, then just make the figure invisible and return.
%
% It's possible the window doesn't even exist yet, so use try/catch
switch ( gdata.dim_type )
case { 'TYX', 'YX' }
try

	set ( gdata.vslice_figure, 'visible', 'off' );

catch
	;
end
msg = sprintf ( '%s:  variable %s does not have a depth dimension\n', mfilename, gdata.varname );
rslice_warning ( msg );
return
end




vslice_line_handles = findobj ( gdata.horizontal_slice_axis, 'Tag', 'VSlice Line' );





%
% Turn off zooming
zoom ( gdata.hslice_figure, 'off' );


if use_vslice_vel_line

	%
	% Does it already exist?  If so, get rid of it.  We need a new one.
	if isfield ( gdata, 'vslice_data' )
		if isfield ( gdata.vslice_data, 'vslice_line_handles' )
			try
				delete ( gdata.vslice_data.vslice_line_handles );
			end
		end
	end



	%
	% Get two points on the hslice axis and feed this into
	% rslice_xslice
	axes ( handles.horizontal_slice_axis );
	the_clim = get ( gca, 'clim' );

	spx = gdata.vslice_vel_data.spline_x;
	spy = gdata.vslice_vel_data.spline_y;

	vslice_control_pt_x = gdata.vslice_vel_data.control_pt_x;
	vslice_control_pt_y = gdata.vslice_vel_data.control_pt_y;
	vslice_hires_x = gdata.vslice_vel_data.hires_x;
	vslice_hires_y = gdata.vslice_vel_data.hires_y;

	[vslice_vel_line_handles] =  draw_vslice_line ( vslice_control_pt_x, vslice_control_pt_y, spx, spy );

	set ( vslice_vel_line_handles, 'Tag', 'Vslice Line' );


elseif get_new_line

	%
	% Does it already exist?  If so, get rid of it.  We need a new one.
	if ~isempty ( vslice_line_handles )
		delete(vslice_line_handles);
	end


	%
	% Get two points on the hslice axis and feed this into
	% rslice_xslice
	axes ( handles.horizontal_slice_axis );
	the_clim = get ( gca, 'clim' );

	[spx, spy, vslice_line_handles] = acquire_vslice_line;
	drawnow;
	set ( vslice_line_handles, 'Tag', 'VSlice Line' );

	vslice_control_pt_x = get ( vslice_line_handles(3), 'XData' );
	vslice_control_pt_y = get ( vslice_line_handles(3), 'YData' );
	vslice_hires_x = get ( vslice_line_handles(1), 'XData' );
	vslice_hires_y = get ( vslice_line_handles(1), 'YData' );
	vslice_hires_x = vslice_hires_x(:);
	vslice_hires_y = vslice_hires_y(:);


elseif ~isfield ( gdata, 'vslice_data' )

	%
	% Do we even have any vslice data yet?  If not, just return.
	%fprintf ( 1, ' %s returning early...\n', mfilename );
	return
else

	%
	% Just retrieve the previously stored line data.
	spx = gdata.vslice_data.spline_x;
	spy = gdata.vslice_data.spline_y;

	vslice_control_pt_x = gdata.vslice_data.control_pt_x;
	vslice_control_pt_y = gdata.vslice_data.control_pt_y;
	vslice_hires_x = gdata.vslice_data.hires_x;
	vslice_hires_y = gdata.vslice_data.hires_y;
	vslice_hires_x = vslice_hires_x(:);
	vslice_hires_y = vslice_hires_y(:);

	axes ( gdata.horizontal_slice_axis );
	hold on
	[vslice_line_handles] = ...
		draw_vslice_line ( vslice_control_pt_x, vslice_control_pt_y, spx, spy );

	set ( vslice_line_handles, 'Tag', 'VSlice Line' );
end

if retrieve_new_data

	if ~isfield ( gdata, 'grid' )
		[grid.x,grid.y,grid.z,grid.mask,status] = rslice_get_grid ( gdata );
		gdata.grid = grid;
	end
	[vx,vz,vdata,transect_dist] = rslice_xslice (vslice_hires_x, vslice_hires_y, gdata.grid, gdata );

else

	%
	% Just use what should already be available to us.
	vx = gdata.vslice_data.vx;
	vz = gdata.vslice_data.vz;
	vdata = gdata.vslice_data.vdata;
	transect_dist = gdata.vslice_data.transect_dist;

end


%
% figure out where the control points fall onto the bottom
Nh = length(vslice_hires_x);
Nl = length(vslice_control_pt_x);
t = linspace ( 0, 1, Nh )';
t_inds = round ( linspace ( 1, Nh, Nl ) );
t_control_pts = t(t_inds);

cpsx = spline ( t, [0; transect_dist(:); 0] );
lrx = ppval ( cpsx, t_control_pts );

bottom_hires_z = vz(end,:)';
if all(isnan(bottom_hires_z))
	rslice_error ( 'Bottom bathymetries were all NaN' );
end
cpsz = spline ( t, [0; vz(end,:)'; 0] );
lrz = ppval ( cpsz, t_control_pts );



set ( gdata.vslice_figure, 'visible', 'on' );


figure ( gdata.vslice_figure );

%
% Delete all the axis elements
prior_h = findobj ( gdata.vslice_figure, 'type', 'axes' );
delete(prior_h);


%
% STore the data for the next time around.
vslice_data.vx = vx;
vslice_data.vz = vz;
vslice_data.vdata = vdata;
vslice_data.vslice_line_handles = vslice_line_handles;
vslice_data.transect_dist = transect_dist;
vslice_data.spline_x = spx;
vslice_data.spline_y = spy;
vslice_data.control_pt_x = vslice_control_pt_x;
vslice_data.control_pt_y = vslice_control_pt_y;
vslice_data.hires_x = vslice_hires_x;
vslice_data.hires_y = vslice_hires_y;


gdata.vslice_data = vslice_data;




try

    if vslice_handles.autoscale
        clim = [min(vdata(:)) max(vdata(:))];
    else
        clim = get ( gdata.horizontal_slice_axis, 'CLim' );
    end

catch

    %
    % The vslice_handles probably didn't have autoscale 
    % defined yet, so we assume no autoscaling.
    % Get the new colorlimits from the horizontal slice.
    clim = get ( gdata.horizontal_slice_axis, 'CLim' );

end


figure ( gdata.vslice_figure );
gcf = gdata.vslice_figure;
vslice_axis = axes ( 'parent', gdata.vslice_figure, 'Position', [0.13 0.11 .6921 0.815] );
axes(vslice_axis);
h = pcolor ( vx, vz, vdata ); 

%
% Sometimes they get confused as to where they belong.
set ( h, 'parent', vslice_axis );
shading ( vslice_axis, 'flat' ); set ( vslice_axis, 'Clim', clim );



hold on
h1 = plot ( lrx, lrz, 'o' );
set ( h1, 'MarkerSize', 8 );
set ( h1, 'Color', 'k' );
set ( h1, 'parent', vslice_axis );
h2 = plot ( lrx, lrz, 'w*' );
set ( h2, 'parent', vslice_axis );
h3 = plot ( transect_dist, vz(end,:) );
set ( h3, 'parent', vslice_axis );



%
% Delete the old colorbar and put in a new one
h = colorbar;
set ( h, 'Tag', 'VSlice Colorbar', 'parent', gdata.vslice_figure, 'Position', [0.831 0.11 0.0607 0.815] );



guidata ( hObject, gdata );









